﻿using Elements;
using Elements.Links;
using Elements.Shapes;
using FlowChartCanvas;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FlowChartDemo
{
    public partial class FormDemo08V1 : FormBase
    {
        public FormDemo08V1()
        {
            InitializeComponent();
            DemoTitle = "第10节随课Demo  Part1";
            DemoNote = "效果：增加贝塞尔曲线。";

            //添加画布控件
            _fcc = new FCCanvasV2();
            _fcc.FCC_LinkColor += _fcc_FCC_LinkColor;
            _fcc.FCC_LinkState += _fcc_FCC_LinkState;
            _fcc.Dock = DockStyle.Fill;
            panel1.Controls.Add(_fcc);

        }

        private void _fcc_FCC_LinkState(string obj)
        {
            toolStripStatusLabel1.Text = obj;
        }

        private Color _fcc_FCC_LinkColor()
        {
            return GetColor(_linkColorIndex++);
        }

        FCCanvasV2 _fcc;

        /// <summary>
        /// 形状颜色序号
        /// </summary>
        int _shapeColorIndex = 0;
        /// <summary>
        /// 连线颜色序号
        /// </summary>
        int _linkColorIndex = 0;
               
        /// <summary>
        /// 获取不同的背景颜色
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        Color GetColor(int i)
        {
            switch (i)
            {
                case 0: return Color.Red;
                case 1: return Color.Green;
                case 2: return Color.Blue;
                case 3: return Color.Orange;
                case 4: return Color.Purple;
                default: return Color.Red;
            }
        }

        //注：文章中说明：再次抽象

        /// <summary>
        /// 创建形状
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="shapeText"></param>
        /// <returns></returns>
        T CreateShape<T>(string shapeText) where T:ShapeBase
        {
            var t = Activator.CreateInstance<T>();
            t.Id = shapeText + Guid.NewGuid().ToString();
            t.Rect = new Rectangle()
            {
                X = 50,
                Y = 50,
                Width = 100,
                Height = 100,
            };
            t.FontColor = Color.White;
            t.BackgroundColor = GetColor(_shapeColorIndex++);
            t.Text = shapeText + _shapeColorIndex;
            t.TextFont = Font;
            return t;
        }

        /// <summary>
        /// 创建指定类型的形状并添加到当前流程图画布中。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="shapeText"></param>
        void CreateShapeAndAddToFCCanvas<T>(string shapeText) where T : ShapeBase
        {
            var sp = CreateShape<T>(shapeText);

            _fcc.FCC_AddShapes(new List<ShapeBase>() { sp });
            _fcc.FCC_Refresh();
        }

        private void FormDemo08V1_Load(object sender, EventArgs e)
        {
            toolStripComboBox1.SelectedIndex = 0;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            CreateShapeAndAddToFCCanvas<RectShapeV2>("矩形");
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            CreateShapeAndAddToFCCanvas<EllipseShapeV2>("圆形");
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            CreateShapeAndAddToFCCanvas<LozengeShapeV2>("菱形");
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            CreateShapeAndAddToFCCanvas<ParallelogramShapeV2>("平行四边形");
        }

        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            CreateShapeAndAddToFCCanvas<RoundRectShapeV2>("圆角矩形");
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            _fcc.FCC_StartLink();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            _fcc.FCC_StopLink();
        }

        private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (toolStripComboBox1.SelectedIndex < 0) return;
                        
            if (toolStripComboBox1.SelectedItem.ToString() == "直线")
            {
                _fcc.FCC_LinkType = "LineLink";
            }
            else if (toolStripComboBox1.SelectedItem.ToString() == "贝塞尔曲线")
            {
                _fcc.FCC_LinkType = "BezierLink";
            }
        }
    }


}
